﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core;
using Telerik.Web.UI;
using System.Web.UI;
using System.Collections.ObjectModel;

namespace Medianamik.UI.Controls
{
    public class DropTreeView : BaseMedianamikControl, IMedianamikControl
    {
        private RadComboBox _combo = new RadComboBox();
        private RadTreeView _tree = new RadTreeView();
        private RadComboBoxItem _item = new RadComboBoxItem();
        protected override void CreateControlHierarchy(bool pUseDataSource)
        {

            _tree.NodeExpand += new RadTreeViewEventHandler(_tree_NodeExpand);

            Controls.Add(_combo);
            _combo.Items.Add(_item);
            _item.Controls.Add(_tree);

            //Contruire l'arbre APRES avoir ajouté le TreeView à la hierarchie
            //Pour que le ViewState des TreeNodes soit enregistré
            if (pUseDataSource)
            {
                BuildTree();
            }
        }

        private List<ContentNode> _rootNodes;
        public List<ContentNode> RootNodes
        {
            get
            {
                if (_rootNodes == null)
                {
                    _rootNodes = NodeManager.GetChildNodes<ContentNode>(MedianamikModules.ContentManagement);
                    _rootNodes.Sort((n1, n2) => n1.LongPosition.CompareTo(n2.LongPosition));
                }
                return _rootNodes;
            }
        }


        private void BuildTree()
        {
            string allOptions = ".";
            foreach (RelationOption rel in Options)
                allOptions += rel.NodeId.ToString() + ".";

            foreach (ContentNode option in RootNodes)
            {
                RadTreeNode nd = new RadTreeNode(option.DisplayName + option.Depth.ToString(), option.NodeType.ID.ToString());
                if (option.HasChildren)
                    nd.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                _tree.Nodes.Add(nd);

            }
        }

        void _tree_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            List<ContentNode> childs;
            childs = NodeManager.GetChildNodes<ContentNode>(new Guid(e.Node.Value));

            foreach (ContentNode child in childs)
            {
                RadTreeNode nd = new RadTreeNode(child.DisplayName + child.Depth.ToString(), child.NodeType.ID.ToString());
                if (child.HasChildren)
                    nd.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                e.Node.Nodes.Add(nd);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            BeginRenderControl(writer);
            _combo.RenderControl(writer);
            EndRenderControl(writer);

        }

        #region Abstract Members

        public override Type TypeOfValue
        {
            get { return typeof(Guid?); }
        }

        public override object GetValue()
        {
            //Guid g;
            return null;//_ddList.Items.Cast<ListItem>().Where(i => i.Selected && StringHelper.IsGuid(i.Value, out g)).Select(i => (Guid?)new Guid(i.Value))
            //.FirstOrDefault();
        }
        #endregion

    }

}
